package net.xju3_4.controller;

import net.xju3_4.common.framework.simplemvc.RequestMapping;
import net.xju3_4.common.utils.JSONUtil;
import net.xju3_4.common.utils.PageInfo;
import net.xju3_4.common.utils.Result;
import net.xju3_4.entity.Orders;
import net.xju3_4.service.OrdersService;
import net.xju3_4.service.impl.OrdersServiceImpl;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.UnsupportedEncodingException;
import java.sql.Time;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;

@RequestMapping("/api/orders")
public class OrdersController {
    OrdersService ordersService = new OrdersServiceImpl();

    // 添加
    @RequestMapping("/insert.do")
    public void insert(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");

        Orders orders = new Orders();
        orders.setOrderNumber(req.getParameter("orderNumber"));
        orders.setTypeid(Integer.parseInt(req.getParameter("typeid")));
        orders.setCustomerid(Integer.parseInt(req.getParameter("customerid")));
        orders.setStartTime(Time.valueOf(req.getParameter("startTime")));
        orders.setEndTime(Time.valueOf(req.getParameter("endTime")));
        orders.setRouteDescription(req.getParameter("routeDescription"));
        orders.setTotalDistance(req.getParameter("totalDistance")==null?0.0:Double.parseDouble(req.getParameter("totalDistance")));
        orders.setStatus(req.getParameter("status"));
        orders.setTotalAmount(Double.parseDouble(req.getParameter("totalAmount")));
        orders.setPaymentStatus(req.getParameter("paymentStatus"));
        orders.setCreatedAt(LocalDateTime.now());
        ordersService.insert(orders);
        JSONUtil.printByJSON(resp, Result.success("添加成功"));
    }
    // 根据id查询用户
    @RequestMapping("/selectOne.do")
    public void selectOne(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        Orders orders = ordersService.selectOne(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success(orders));
    }
    // 删除
    @RequestMapping("/deleted.do")
    public void deleteById(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        String id = req.getParameter("id");
        ordersService.deleteById(Integer.parseInt(id));
        JSONUtil.printByJSON(resp, Result.success("删除成功"));
    }
    // 修改
    @RequestMapping("/update.do")
    public void update(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        req.setCharacterEncoding("utf-8");
        Orders orders = new Orders();
        orders.setId(Integer.parseInt(req.getParameter("id")));

        orders.setOrderNumber(req.getParameter("orderNumber"));
        orders.setTypeid(Integer.parseInt(req.getParameter("typeid")));
        orders.setCustomerid(Integer.parseInt(req.getParameter("customerid")));
        orders.setStartTime(Time.valueOf(req.getParameter("startTime")));
        orders.setEndTime(Time.valueOf(req.getParameter("endTime")));
        orders.setRouteDescription(req.getParameter("routeDescription"));
        orders.setTotalDistance(Double.parseDouble(req.getParameter("totalDistance")));
        orders.setStatus(req.getParameter("status"));
        orders.setTotalAmount(Double.parseDouble(req.getParameter("totalAmount")));
        orders.setPaymentStatus(req.getParameter("paymentStatus"));

        // orders.setCreateTime(LocalDateTime.now());
        ordersService.update(orders);
        JSONUtil.printByJSON(resp,Result.success("修改成功"));
    }

    // 条件分页查询
    @RequestMapping("/list.do")
    public void list(HttpServletRequest req, HttpServletResponse resp) throws UnsupportedEncodingException {
        // 控制层-》业务层-》持久层
        req.setCharacterEncoding("utf-8");
        // 条件查询参数
        String orderNumber = req.getParameter("orderNumber");
        String typeid = req.getParameter("typeid");
        String customerid = req.getParameter("customerid");
 